package javablackbelt.i18n;

import java.util.Locale;

public class LocaleHolder {

	private static final LocaleThreadLocal locale = new LocaleThreadLocal("Current Locale");
	
	public static void setUserLocale(Locale newLocale) {
		
		if (newLocale ==  null) {
			throw new IllegalArgumentException("The threadLocal name must not be empty.");
		}
		
		if (newLocale.equals(locale.get())) {
			return;
		}
		locale.set(newLocale);
	}
	
	public static Locale getLocale() {
		return locale.get();
	}
	
	private static class LocaleThreadLocal extends ThreadLocal<Locale> {
		
		private String name;
		
		public LocaleThreadLocal(String name) {
			
			if (name == null | "".equals(name)) {
				throw new IllegalArgumentException("The threadLocal name must not be empty.");
			}
			this.name = name;
		}

		public String toString() {
			return this.name;
		}
	}
}
